package com.taxigang.client.facebook;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.taxigang.client.Authenication;
import com.taxigang.client.Authenication.AuthStatus;
import com.taxigang.client.Authenication.Provider;
import com.taxigang.client.Authenication.Status;
import com.taxigang.client.Session;
import com.taxigang.client.micro.TaxiGangCreator;
import com.taxigang.client.ui.LayoutConstants;
import com.taxigang.client.uibinder.TaxiGangPresenter;
import com.taxigang.shared.LoginInfo;

/**
 * Provides responses for queries made 
 */

public class GetUserDetailsCallback implements AsyncCallback<ConnectState> {

	private ThenGetUserDetails thenGetUserDetails;
	private boolean showLoginOnLoggedOut;
	public GetUserDetailsCallback(ThenGetUserDetails thenGetUserDetails, boolean showLoginOnLoggedOut){
		this.thenGetUserDetails = thenGetUserDetails;
		this.showLoginOnLoggedOut = showLoginOnLoggedOut;
	}
	public void onFailure(Throwable caught) {
		Window.alert ( "Failed to get facebook connect status, hit reload" );
	}
	public void onSuccess(ConnectState result) {
		Log.info("GetUserDetailsCallback onSuccess connected="+(result == ConnectState.connected));
		Authenication authenication = Authenication.getInstance();
		//microLoadingWidget.hide();
		if ( result == ConnectState.connected ) {
			if (authenication.getLogedinResolved(Provider.Facebook, Status.LoggedIn)){		
				if (authenication.getAuthStatus()==AuthStatus.LoggedInFacebook){
					LayoutConstants.getInstance().setSession(Session.getInstance());

					//Window.alert ("fb-in");
					//dont' ask details if logged into fb
					LoginInfo fbloginInfo = Session.getInstance().getLoginInfo();
					fbloginInfo.setLoginProvider(Provider.Facebook.toString());

					fbloginInfo.setLoggedIn(true);
					fbloginInfo.setIsTempory(false);
					FacebookApi.waitUntilUserDetailsReady(thenGetUserDetails);

//					if (Session.getInstance().getIsUserAgentMobile()){
//						FacebookApi.waitUntilUserDetailsReady(new ThenGetUserDetailsMobileCreate());
//					} else {
//						FacebookApi.waitUntilUserDetailsReady(new ThenGetUserDetails());
//					}

				}
			}
		} else {
			if (authenication.getLoggedoutResolved(Provider.Facebook, Status.LoggedOut)){					
			} else {
			}
			TaxiGangPresenter.getInstance().authenticationComplete(showLoginOnLoggedOut);
		}

		//layoutWelcome(loginInfo);
	}



};
